from dataclasses import dataclass
from enum import Enum
from typing import Any, Dict, List


class SourceType(Enum):
    DATABASE = "database"
    EXCEL = "excel"
    CSV = "csv"
    JSON = "json"


@dataclass
class DataSource:
    """数据源配置"""

    type: SourceType
    path: str  # 添加这个，用于存储文件路径
    field_mapping: Dict[str, str]  # 字段映射关系
    db_config: Dict[str, Any]  # 添加数据库配置

    def __init__(
        self, type: SourceType, path: str, mapping: dict = None, db_config: dict = None
    ):
        self.type = type
        self.path = path
        self.field_mapping = mapping or {}
        self.db_config = db_config or {}  # 添加数据库配置

    def to_dict(self) -> dict:
        """转换为字典"""
        return {
            "type": self.type.value,
            "path": self.path,
            "mapping": self.field_mapping,
            "db_config": self.db_config,  # 添加数据库配置的序列化
        }

    @classmethod
    def from_dict(cls, data: dict) -> "DataSource":
        """从字典创建实例"""
        return cls(
            type=SourceType(data["type"]),
            path=data["path"],
            mapping=data.get("mapping", {}),
            db_config=data.get("db_config", {}),  # 添加数据库配置的反序列化
        )


@dataclass
class CompareConfig:
    """对比配置"""

    fields: List[
        Dict[str, str]
    ]  # 数据项定义列表 [{"name": "xx", "code": "xx", "description": "xx"}, ...]
    id_fields: List[str]  # 主键字段列表
    source1: DataSource  # 数据源1
    source2: DataSource  # 数据源2
    show_all: bool = False  # 是否显示全部数据
